package com.ggk911.pharmacy_system.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.Purchase;
import com.ggk911.pharmacy_system.entity.PurchaseInfo;
import com.ggk911.pharmacy_system.service.impl.PurchaseInfoServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购单详情控制层
 *
 * @author ggk-911
 * @version 1.0 2023/3/20 10:16
 */
@RestController
@RequiredArgsConstructor
@CrossOrigin
@Slf4j
@Api(tags = "订单详情信息控制层")
@RequestMapping("/purchaseInfo")
public class PurchaseInfoController {
    private final PurchaseInfoServiceImpl service;

    //GET查询
    @GetMapping("/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               @RequestParam(defaultValue = "-1") Integer purchaseId) {

        Page<PurchaseInfo> purchaseInfoPage = new Page<>(page, size);
        LambdaQueryWrapper<PurchaseInfo> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(purchaseId != -1, PurchaseInfo::getPurchaseId, purchaseId); //查询单个订单

        service.page(purchaseInfoPage, wrapper);

        return RestResult.ok("查询成功", purchaseInfoPage);
    }

    //GET查询倒计时time天过期药品数量
    @GetMapping("/getDueDrug")
    public RestResult getDueDrug() {
        int dueDate = service.getDueDate();
        //筛选日期和无日期的，无日期的即处理过的药品，短路运算，不然要空指针
        List<PurchaseInfo> purchaseInfoList = service.list().stream().filter(item -> item.getDrugInfoProductionDate() != null && DateUtil.between(DateUtil.offsetDay(item.getDrugInfoProductionDate(), item.getSupplierDrug().getDrugInfo().getDrugInfoValidTime()), new Date(), DateUnit.DAY) <= dueDate).collect(Collectors.toList());
        return RestResult.ok("查询成功", purchaseInfoList);
    }

    //PUT修改倒计时
    @PutMapping("/updateDueDate")
    public RestResult updateDueDate(Integer day) {
        if (service.updateDueDate(day)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }

    //GET获取倒计时天数
    @GetMapping("/getDueDay")
    public RestResult getDueDay() {
        return RestResult.ok("查询成功", service.getDueDate());
    }

    //PUT更新生产日期，过期处理
    @PutMapping("/dueDrugCheck")
    public RestResult dueDrugCheck(Integer id) {
        if (service.setDrugInfoProductionDate(id)) {
            return RestResult.ok("过期处理成功");
        }
        return RestResult.error("过期状态异常");
    }

    //GET获取全部
    @GetMapping("/list")
    public RestResult list(@RequestParam(defaultValue = "-1") Integer purchaseId) {
        LambdaQueryWrapper<PurchaseInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(purchaseId != -1, PurchaseInfo::getPurchaseId, purchaseId);

        return RestResult.ok("查询成功", service.list(wrapper));
    }


    //PUT添加
    @PutMapping("/insert")
    public RestResult insert(PurchaseInfo purchaseInfo) {
        if (service.save(purchaseInfo)) {
            return RestResult.ok("添加成功，返回订单编号为：" + purchaseInfo.getPurchaseInfoId(), purchaseInfo.getPurchaseInfoId());
        }
        return RestResult.error("添加失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer id) {
        if (service.removeById(id)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }

    //DELETE批量删除
    @DeleteMapping("/deleteList")
    public RestResult deleteList(Integer id) {
        if (service.removeByPurchaseId(id)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }

    //POST修改
    @PostMapping("/update")
    public RestResult update(PurchaseInfo purchaseInfo) {
        if (service.updateById(purchaseInfo)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }
}
